"""Main Controller"""
from authoritytestapp.lib.base import BaseController
from tg import expose, flash
from pylons.i18n import ugettext as _
#from tg import redirect, validate
#from authoritytestapp.model import DBSession

# Supports the copyright date in the footer template
from datetime import datetime
now=datetime.now()

from authority.require import require, require2
#~ from authority.conditions import

import logging
log = logging.getLogger(__name__)

class RootController(BaseController):

    @expose('authoritytestapp.templates.index')
    def index(self, *args, **kwargs):
        log.debug('index writing from logger')
        flash(_("Welcome to the Authority Test App!"))
        return dict(now=now)

    @expose('authoritytestapp.templates.private')
    @require
    def private(self, *args, **kwargs):
        flash(_("This is a private page"))
        return dict(now=now)

    @expose('authoritytestapp.templates.private')
    @require2()
    def private2(self, *args, **kwargs):
        #~ self._attach_locals()
        flash(_("This is a private (2) page"))
        return dict(now=now)

    @expose('authoritytestapp.templates.formpage')
    def loginform(self, *args, **kwargs):
        flash(_("This is a login form"))
        return dict(now=now)

    @expose('authoritytestapp.templates.index')
    def redirected(self):
        redirect('/target')

    @expose('authoritytestapp.templates.index')
    def target(self):
        flash(_("Target of redirect attempt"))
        log.debug('target writing from logger')
        #~ stderr.write('index writing on stderr')
        return dict(now=now)

    # TODO: this doesn't work yet!
    @expose('authoritytestapp.templates.index')
    def logout(self):
        del session['authority.user']
        session.save()
        flash(_("You have logged out"))
        return dict(now=now)
